package com.chaoyong.higo.adapter;

import java.util.ArrayList;
import java.util.List;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseAdapter;
import com.chaoyong.higo.bean.GoodsRecordBean_z;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.utils.ViewHolder;
import com.chaoyong.higo.view.PurchaseRecordDialog;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 云筹记录
 * 
 * @author zk
 *
 */
public class PurchaseRecordAdapter extends BaseAdapter<GoodsRecordBean_z.DataEntity.ListEntity> {

	/** 商品图片 */
	private ImageView record_goods_iv;
	/** 商品描述 */
	private TextView record_goods_tv;
	/** 商品价格 */
	private TextView record_price_tv;
	/** 参与次数 */
	private TextView record_part_in_tv;
	/** 所有云筹码 */
	private TextView record_all_code_tv;

	//private List<String> codes = new ArrayList<>();

	public PurchaseRecordAdapter(Context context) {
		super(context);
		OApi.elist = mList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_records, null);
		}

		findview(convertView);

		String id = mList.get(position).getId();
		String goods_name = mList.get(position).getGoods_name();
		String goods_expect = mList.get(position).getExpect();
		String goods_price = mList.get(position).getGoods_price();
		
		String buy_money = mList.get(position).getBuy_money();
		
		List<String> uri = mList.get(position).getCover_img();
		final List<String> codes = mList.get(position).getWinning();

		/*codes.remove(codes.size() - 1);
		
		List<String> codess = codes;*/
		
		if (!EmptyUtil.isEmpty(buy_money)) {
			record_part_in_tv.setText(buy_money);
		} else {
			record_part_in_tv.setText("--");
		}

		if (!EmptyUtil.isEmpty(goods_price)) {
			record_price_tv.setText(goods_price);
		} else {
			record_price_tv.setText("");
		}

		if (!EmptyUtil.isEmpty(goods_name)) {
			record_goods_tv.setText("(第" + goods_expect + mContext.getResources().getString(R.string.expect) + ")" + goods_name);
		} else {
			record_goods_tv.setText("");
		}

		if (!EmptyUtil.isEmpty(uri)) {
			ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + uri.get(0), record_goods_iv);
		}

		record_all_code_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				 Toast.makeText(mContext, mList.size() + "", Toast.LENGTH_SHORT).show();
				
				showDialog(codes);
			}
		});

		return convertView;
	}

	private void findview(View convertView) {
		record_goods_iv = ViewHolder.get(convertView, R.id.record_goods_iv);
		record_goods_tv = ViewHolder.get(convertView, R.id.record_goods_tv);
		record_price_tv = ViewHolder.get(convertView, R.id.record_price_tv);
		record_part_in_tv = ViewHolder.get(convertView, R.id.record_part_in_tv);
		record_all_code_tv = ViewHolder.get(convertView, R.id.record_all_code_tv);
	}

	private void showDialog(List<String> codes) {
		PurchaseRecordDialog d = new PurchaseRecordDialog(mContext,codes);
		d.setCancelable(false);
		d.show();
	}
}
